﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Scheme.Net.Runtime.AST;
using Scheme.Net.Runtime.Evaluator;

namespace Scheme.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<IEvaluable> read = () =>
            {
                var nOpen = 1;
                var nClose = 0;
                var pr = "";
                var prompt = "> ";
                while (nOpen > nClose)
                {
                    Console.Write(prompt);
                    pr += Console.ReadLine();
                    nOpen = pr.Where(c => c == '(').Count();
                    nClose = pr.Where(c => c == ')').Count();
                    prompt = ". ";
                }
                if (nClose > nOpen)
                    throw new EvaluatorException("Unbalanced brackets");
                return Runtime.Parser.SchemeParser.Parse(pr);
            };

            Action<IEnumerable<IEvaluable>> write = vals =>
            {
                foreach (var x in vals)
                    if (x == Symbol.Unspecified)
                        Console.WriteLine(";Unspecified return value");
                    else
                        Console.WriteLine(";Value: {0}", x);
            };

            Console.WriteLine("Scheme.Net Evaluator v1.0 (c) 2012 George Tryfonas");
            Console.WriteLine("Evaluating standard library...");
            var e = new Evaluator(read, write);
            e.Evaluate(Pair.CreateList(new IEvaluable[] { Symbol.Lookup("LOAD"), new Runtime.AST.String("library.scm") }));

            e.Repl();
        } 
    }
}
